<template>
  <div class="page">
    <v-pageTitle title="修改密码"></v-pageTitle>
    <el-row type="flex" justify="space-around" style="margin-top:50px;">
      <el-col :span="11">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <h4>用户信息</h4>
          </div>
          <el-form :model="ruleForm" :rules="rule" ref="ruleForm" label-width="90px" class="demo-ruleForm">
            <el-form-item label="用户名">
              <el-input v-model="username" readonly></el-input>
            </el-form-item>
            <el-form-item label="原密码" prop="oldPassword">
              <el-input type="password" v-model="ruleForm.oldPassword" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input type="password" v-model="ruleForm.newPassword" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="checkPassword">
              <el-input type="password" v-model="ruleForm.checkPassword" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button  type="primary" @click="submitForm('ruleForm')" :loading="loading">提交</el-button>
              <el-button  @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapState, mapMutations, mapActions } from 'Vuex'
import panel from "@/components/common/panel.vue"
import util from '@/util/util'
  export default {
    components: {
      'imp-panel': panel
    },
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPassword !== '') {
            this.$refs.ruleForm.validateField('checkPassword');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else if (!((/^.{8,16}$/).test(value))) {
          callback(new Error('密码长度必须为8到16位!'));
        }else{
          callback();
        }
      };
      return {
        loading: false,

        user: {
          id:'',
          username: ''
        },
        ruleForm: {
          oldPassword: '',
          newPassword: '',
          checkPassword: ''
        },
        rule: {
          newPassword: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPassword: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapActions({
        sysLogout: 'user/logout'
      }),
      baseInfo(){
        let this_ = this;
        let url = 'base/config/baseUser';
        util.httpJesenGet(url,'',function(data){
          this_.user.id = data.data.id;
          this_.user.username = data.data.username;
        });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let this_ = this;
            this_.loading = true;
            let params = {
              'id': this_.user.id,
              'username':this_.user.username,
              'oldpassword':this_.ruleForm.oldPassword,
              'password':this_.ruleForm.newPassword
            };
            let url = 'base/baseUser/updatePassWord';
            
            util.httpJesen(url,params,function(data){
              this_.loading = false;
              this_.$message({
                message: '修改成功',
                type: 'success'
              });
              // 登出
              this_.sysLogout().then(() => {
                this_.$router.push('/login')
                window.location.reload()
              })
            });

            setTimeout(function(){
              this_.loading = false;
            },2000);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted() {
      this.baseInfo();
    },
    computed: {
        ...mapState({
            username: state => state.user.name
        })
    }
  }
</script>